package ldh.im.fxgui.component.cell;

import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import ldh.im.fxbase.data.pojo.ImGroup;
import ldh.im.fxbase.data.pojo.ImSession;
import ldh.im.fxbase.data.pojo.ImUser;
import ldh.im.fxbase.util.ImCacheFactory;
import ldh.im.fxgui.pojo.WrapSearchResult;
import ldh.im.fxgui.util.UiUtil;
import ldh.im.fxgui.util.UserHeadImageUtil;

public class SearchResultView extends GridPane {

    private WrapSearchResult wrapSearchResult;

    public SearchResultView(WrapSearchResult wrapSearchResult) {
        this.wrapSearchResult = wrapSearchResult;

        this.getStyleClass().add("search-result-view");
        initUi();
    }

    public void initUi() {
        if (wrapSearchResult.isTitle()) {
            Label label = new Label(wrapSearchResult.getTitle());
            GridPane.setConstraints(label, 0, 0, 2, 1);
            this.getChildren().add(label);
        } else if (wrapSearchResult.isButton()) {
            GridPane.setConstraints(wrapSearchResult.getJfxButton(), 0, 0, 2, 1);
            this.getChildren().add(wrapSearchResult.getJfxButton());
        } else {
            if (wrapSearchResult.getSearchResult().isUser()) {
                buildUserUi();
                return;
            }

            if (wrapSearchResult.getSearchResult().isGroup()) {
                buildGroupUi();
            }

            if (wrapSearchResult.getSearchResult().isGroupUser()) {
                buildGroupUserUi();
            }

            if (wrapSearchResult.getSearchResult().isMessage()) {
                buildSessionUi();
            }
        }
    }

    private void buildUserUi() {
        ImUser imUser = ImCacheFactory.getInstance().getUser(wrapSearchResult.getSearchResult().getUserId());
        ImageView headImageView = UserHeadImageUtil.createUserImage(imUser);
        headImageView.setFitWidth(40);
        headImageView.setFitHeight(40);

        GridPane.setConstraints(headImageView, 0, 0, 1, 1);

        Label label = new Label(imUser.getUserName());
        GridPane.setConstraints(label, 1, 0, 1, 1);

        this.getChildren().addAll(headImageView, label);
    }

    private void buildGroupUi(){
        String userId = UiUtil.getLoginImUser().getUserId();
        ImGroup imGroup = ImCacheFactory.getInstance().getImGroup(userId, wrapSearchResult.getSearchResult().getGroupCode());
        Pane headImageView = UserHeadImageUtil.createGroupImage(imGroup);

        GridPane.setConstraints(headImageView, 0, 0, 1, 2);

        Label groupName = new Label(imGroup.getName());
        GridPane.setConstraints(groupName, 1, 0, 1, 1);

        Label findTotalLabel = new Label("包含:");
        GridPane.setConstraints(findTotalLabel, 1, 1, 1, 1);

        this.getChildren().addAll(headImageView, groupName, findTotalLabel);
    }

    private void buildGroupUserUi(){
        String userId = UiUtil.getLoginImUser().getUserId();
        ImGroup imGroup = ImCacheFactory.getInstance().getImGroup(userId, wrapSearchResult.getSearchResult().getGroupCode());
        Pane headImageView = UserHeadImageUtil.createGroupImage(imGroup);

        GridPane.setConstraints(headImageView, 0, 0, 1, 2);

        Label groupName = new Label(imGroup.getName());
        GridPane.setConstraints(groupName, 1, 0, 1, 1);

        Label findTotalLabel = new Label("包含:" + wrapSearchResult.getSearchResult().getUserName());
        GridPane.setConstraints(findTotalLabel, 1, 1, 1, 1);

        this.getChildren().addAll(headImageView, groupName, findTotalLabel);
    }

    private void buildSessionUi(){
        ImSession imSession = wrapSearchResult.getSearchResult().getImSession();
        Node headImageView = UserHeadImageUtil.createSessionImage(imSession);

        GridPane.setConstraints(headImageView, 0, 0, 1, 2);

        Label sessionName = new Label(imSession.getName());
        GridPane.setConstraints(sessionName, 1, 0, 1, 1);

        Label findTotalLabel = new Label(wrapSearchResult.getSearchResult().getMessageTotal() + "条相关聊天记录");
        GridPane.setConstraints(findTotalLabel, 1, 1, 1, 1);

        this.getChildren().addAll(headImageView, sessionName, findTotalLabel);
    }
}
